<?php
class GuestbookModel extends Model {
	public function __construct() {
		parent::__construct('Guestbook');
	}
	
	public function getOneByPk($id, $field = '*') {
		return $this->field($field)->where('id='.$id)->find();
	}
	
	public function addOne(& $data) {
		return $this->data($data)->add();
	}
	
	public function deleteByCondition($where) {
		return $this->where($where)->delete();
	}
	
	public function getByCondition($where) {
		return $this->where($where)->select();
	}
	
	// 留言倒叙排列
	public function getListByPage($pageSize = 10) {
		import('ORG.Util.Page');
		$pre = C('DB_PREFIX');
		
		$count = $this->where('pid=0')->count();	
		if(empty($count)) $count = 0;
		$page = new Page($count, $pageSize);
		$pageString = $page->show();
		
		$sql = "select id,nickname,email,website,content,tongguo,posttime,(select count(*) from {$pre}guestbook as r where r.pid={$pre}guestbook.id) as c".
			" from {$pre}guestbook".
			" where pid=0 and id<=(select id from {$pre}guestbook order by id desc limit {$page->firstRow},1) order by id desc limit {$pageSize}";
			
		$data = $this->query($sql);
		//var_dump($this->getLastsql()); exit;
		
		$data['pageString'] = $pageString;
		
		return $data;
	}
}